<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="后端,PHP,">










<meta name="description" content="安装docker123456789101112131415#安装依赖包$ yum install -y yum-utils device-mapper-persistent-data lvm2#设置yum源$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo#安装docker$">
<meta name="keywords" content="后端,PHP">
<meta property="og:type" content="article">
<meta property="og:title" content="docker部署php项目">
<meta property="og:url" content="https://zhuyunlong2018.gitee.io/2020/05/07/docker部署php项目/index.html">
<meta property="og:site_name" content="边泉小栈">
<meta property="og:description" content="安装docker123456789101112131415#安装依赖包$ yum install -y yum-utils device-mapper-persistent-data lvm2#设置yum源$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo#安装docker$">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2020-05-07T01:55:53.457Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="docker部署php项目">
<meta name="twitter:description" content="安装docker123456789101112131415#安装依赖包$ yum install -y yum-utils device-mapper-persistent-data lvm2#设置yum源$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo#安装docker$">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zhuyunlong2018.gitee.io/2020/05/07/docker部署php项目/">





  <title>docker部署php项目 | 边泉小栈</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">边泉小栈</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">边泉it技术博客</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            Tags
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            Search
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="Searching..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://zhuyunlong2018.gitee.io/2020/05/07/docker部署php项目/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="ZhuYunlong">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="边泉小栈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">docker部署php项目</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-05-07T09:55:53+08:00">
                2020-05-07
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/环境/" itemprop="url" rel="index">
                    <span itemprop="name">环境</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          
          
            <span class="post-meta-divider">|</span>
            <span id="busuanzi_value_page_pv"></span>次阅读
          
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h4 id="安装docker"><a href="#安装docker" class="headerlink" title="安装docker"></a>安装docker</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">安装依赖包</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> yum install -y yum-utils device-mapper-persistent-data lvm2</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">设置yum源</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">安装docker</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> yum install docker-c</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">启动并加入开机启动</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> systemctl start docker</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> systemctl <span class="built_in">enable</span> docker</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">卸载方法</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> yum remove docker  docker-common docker-selinux docker-engine</span></span><br></pre></td></tr></table></figure>
  <a id="more"></a>
<p>`</p>
<h4 id="docker的一些基本操作"><a href="#docker的一些基本操作" class="headerlink" title="docker的一些基本操作"></a>docker的一些基本操作</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">获取镜像</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker pull imageName</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">查看镜像</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker image ls</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker images</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 删除镜像</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker image rm imageId</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看运行中容器</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker container ls</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker ps <span class="comment"># 或者</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看所有容器</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker container ls -a</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker ps -a  <span class="comment">#或者</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 停止运行容器（开启start,删除rm，重启restart）</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker stop containerID[containerName]</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 进入容器内部</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker <span class="built_in">exec</span> -it containerName /bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">例如进入NGINX内部</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker <span class="built_in">exec</span> -it nginx bash</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">使用dockerFile</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker build [选项] &lt;上下文路径/URL/-&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="安装nginx"><a href="#安装nginx" class="headerlink" title="安装nginx"></a>安装nginx</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">拉取Nginx镜像</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker pull nginx</span></span><br><span class="line"><span class="meta">#</span><span class="bash">新增目录</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> mkdir -p /data/nginx/html /data/nginx/logs /data/nginx/conf /data/nginx/conf.d</span></span><br><span class="line"><span class="meta">#</span><span class="bash">创建配置文件</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> vim /data/nginx/conf/nginx.conf</span></span><br><span class="line"></span><br><span class="line">user  nginx;	</span><br><span class="line">worker_processes  1;</span><br><span class="line">error_log  /var/log/nginx/error.log warn;</span><br><span class="line">pid        /var/run/nginx.pid;</span><br><span class="line">events &#123;</span><br><span class="line">	worker_connections  1024;</span><br><span class="line">&#125;</span><br><span class="line">http &#123;</span><br><span class="line">    include       /etc/nginx/mime.types;</span><br><span class="line">    default_type  application/octet-stream;</span><br><span class="line">    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '</span><br><span class="line">    '$status $body_bytes_sent "$http_referer" '</span><br><span class="line">    '"$http_user_agent" "$http_x_forwarded_for"';</span><br><span class="line">    access_log  /var/log/nginx/access.log  main;</span><br><span class="line">    client_max_body_size 50m;</span><br><span class="line">    sendfile        on;</span><br><span class="line">    #tcp_nopush     on;</span><br><span class="line">    keepalive_timeout  65;</span><br><span class="line">    #gzip  on;</span><br><span class="line">    include /etc/nginx/conf.d/*.conf;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">新增/data/nginx/conf.d/www.conf 文件</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> vim /data/nginx/conf.d/www.conf </span></span><br><span class="line"><span class="meta">#</span><span class="bash">fastcgi_pass的IP地址使用docker inspect php-fpm查看fpm容器ip地址</span></span><br><span class="line">server &#123;</span><br><span class="line">    listen       80;</span><br><span class="line">    server_name  localhost;</span><br><span class="line">    #charset koi8-r;</span><br><span class="line">    #access_log  /var/log/nginx/host.access.log  main;</span><br><span class="line">    location / &#123;</span><br><span class="line">        root   /usr/share/nginx/html;</span><br><span class="line">        index  index.php index.html index.htm;</span><br><span class="line">        try_files $uri $uri/ /index.php?$query_string;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    location ~ \.php &#123;</span><br><span class="line">    	#此处的IP应该为docker php暴露IP，可以用docker inspect 容器来查看</span><br><span class="line">        fastcgi_pass   127.0.0.1:9000;</span><br><span class="line">        fastcgi_index  index.php;</span><br><span class="line">        fastcgi_split_path_info ^(.+\.php)(.*)$;</span><br><span class="line">        fastcgi_param   PATH_INFO $fastcgi_path_info;</span><br><span class="line">        fastcgi_param   SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;</span><br><span class="line">        include        fastcgi_params;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">修改权限</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> chmod -R 777 /data/nginx</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">创建容器</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker run -v /data/nginx/html:/usr/share/nginx/html \</span></span><br><span class="line">-v /data/nginx/conf.d:/etc/nginx/conf.d \</span><br><span class="line">-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \</span><br><span class="line">-v /data/nginx/logs:/var/log/nginx \</span><br><span class="line">-v /etc/localtime:/etc/localtime \</span><br><span class="line">-p 80:80 -p 8888:8888 -p 443:443 \</span><br><span class="line">--name nginx -d nginx</span><br></pre></td></tr></table></figure>
<h4 id="安装mysql"><a href="#安装mysql" class="headerlink" title="安装mysql"></a>安装mysql</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">新增目录</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> mkdir -p /data/mysql/conf /data/mysql/data /data/mysql/logs </span></span><br><span class="line"></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker pull mysql:5.7   <span class="comment"># 拉取 mysql 5.7</span></span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker pull mysql       <span class="comment"># 拉取最新版mysql镜像</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">创建配置文件</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> vim /data/mysql/conf/my.cnf</span></span><br><span class="line"></span><br><span class="line">[mysqld]</span><br><span class="line">server-id = 1 #服务Id唯一</span><br><span class="line">port = 3306</span><br><span class="line">log-error    = /var/log/mysql/error.log</span><br><span class="line"><span class="meta">#</span><span class="bash">只能用IP地址</span></span><br><span class="line">skip_name_resolve</span><br><span class="line"><span class="meta">#</span><span class="bash">数据库默认字符集</span></span><br><span class="line">character-set-server = utf8mb4</span><br><span class="line"><span class="meta">#</span><span class="bash">数据库字符集对应一些排序等规则 </span></span><br><span class="line">collation-server = utf8mb4_general_ci</span><br><span class="line"><span class="meta">#</span><span class="bash">设置client连接mysql时的字符集,防止乱码</span></span><br><span class="line">init_connect='SET NAMES utf8mb4'</span><br><span class="line"><span class="meta">#</span><span class="bash">最大连接数</span></span><br><span class="line">max_connections = 300</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">创建error.log</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> touch /data/mysql/logs/error.log</span></span><br><span class="line"><span class="meta">#</span><span class="bash">修改权限</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> chmod -R 777 /data/mysql</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">–name：容器名，此处命名为mysql</span></span><br><span class="line"><span class="meta">#</span><span class="bash">-e：配置信息，此处配置mysql的root用户的登陆密码</span></span><br><span class="line"><span class="meta">#</span><span class="bash">-p：端口映射，此处映射 主机3306端口 到 容器的3306端口</span></span><br><span class="line"><span class="meta">#</span><span class="bash">建立目录映射并设置初始密码</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker run -p 3306:3306 --name mysql \</span></span><br><span class="line">-v /data/mysql/conf:/etc/mysql \</span><br><span class="line">-v /data/mysql/logs:/var/log/mysql \</span><br><span class="line">-v /data/mysql/data:/var/lib/mysql \</span><br><span class="line">-v /etc/localtime:/etc/localtime \</span><br><span class="line">-e MYSQL_ROOT_PASSWORD=minxiu@20200506 \</span><br><span class="line">-d mysql:5.7</span><br></pre></td></tr></table></figure>
<h4 id="安装redis"><a href="#安装redis" class="headerlink" title="安装redis"></a>安装redis</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">新增目录</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> mkdir -p /data/redis/etc /data/redis/data</span></span><br><span class="line"></span><br><span class="line"><span class="meta">$</span><span class="bash"> vim /data/reids/etc/redis.conf</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 填写配置</span></span><br><span class="line"></span><br><span class="line">**#redis开启守护进程，需要注释掉，不然容器无法跑起来**</span><br><span class="line"><span class="meta">#</span><span class="bash">daemonize yes</span></span><br><span class="line">dir /data</span><br><span class="line">logfile /data/redis.log</span><br><span class="line">protected-mode no</span><br><span class="line"><span class="meta">#</span><span class="bash">注释掉，可以远程访问</span></span><br><span class="line"><span class="meta">#</span><span class="bash"><span class="built_in">bind</span> 127.0.0.1</span></span><br><span class="line"><span class="meta">#</span><span class="bash">开启AOF</span></span><br><span class="line">appendonly yes</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 拉取镜像</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker pull redis</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 此处以自定义目录运行</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker run -itd --name redis -p 6379:6379 \</span></span><br><span class="line">-v /data/redis/etc/redis.conf:/usr/local/etc/redis/redis.conf \</span><br><span class="line">-v /data/redis/data/:/data \</span><br><span class="line">-v /etc/localtime:/etc/localtime \</span><br><span class="line">--appendonly yes redis</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">注意，php要读取redis,需要将host修改为dockerIP，可以使用ifconfig docker0来查看</span></span><br></pre></td></tr></table></figure>
<h4 id="安装php7-2"><a href="#安装php7-2" class="headerlink" title="安装php7.2"></a>安装php7.2</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">创建目录</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> mkdir -p /data/php/conf.d</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 进入Dockerfile目录</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> <span class="built_in">cd</span> doc</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 构建php镜像</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker build -t php-swoole:7.2 .</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 单独运行</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker run -v /data/nginx/html:/var/www/html \</span></span><br><span class="line">-v /data/php:/usr/local/etc/php \</span><br><span class="line">-v /etc/localtime:/etc/localtime \</span><br><span class="line">-p 22026:22026 -p 9000:9000 \</span><br><span class="line">-it --name php72 -d php-swoole:7.2</span><br></pre></td></tr></table></figure>
<p>以下为项目需求dockerfile，包含为php添加mysqli、redis、pdo_mysql、swoole扩展和composer环境<br><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">FROM</span> php:<span class="number">7.2</span>-fpm</span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/3.1.6.tar.gz \</span></span><br><span class="line"><span class="bash">    &amp;&amp; tar xfz /tmp/redis.tar.gz \</span></span><br><span class="line"><span class="bash">    &amp;&amp; rm -r /tmp/redis.tar.gz \</span></span><br><span class="line"><span class="bash">    &amp;&amp; mkdir -p /usr/src/php/ext \</span></span><br><span class="line"><span class="bash">    &amp;&amp; mv phpredis-3.1.6 /usr/src/php/ext/redis \</span></span><br><span class="line"><span class="bash">    &amp;&amp; docker-php-ext-install redis \</span></span><br><span class="line"><span class="bash">    mysqli \</span></span><br><span class="line"><span class="bash">    pdo pdo_mysql</span></span><br><span class="line"><span class="comment"># Swoole extension</span></span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> curl -L -o /tmp/swoole.tar.gz https://github.com/swoole/swoole-src/archive/master.tar.gz \</span></span><br><span class="line"><span class="bash">    &amp;&amp; tar -zxvf /tmp/swoole.tar.gz \</span></span><br><span class="line"><span class="bash">    &amp;&amp; rm -f /tmp/swoole.tar.gz \</span></span><br><span class="line"><span class="bash">    &amp;&amp; mv swoole-src-master /usr/src/php/ext/swoole \</span></span><br><span class="line"><span class="bash">    &amp;&amp; docker-php-ext-install swoole</span></span><br><span class="line"><span class="comment"># 安装composer并允许root用户运行</span></span><br><span class="line"><span class="keyword">ENV</span> COMPOSER_ALLOW_SUPERUSER=<span class="number">1</span></span><br><span class="line"><span class="keyword">ENV</span> COMPOSER_NO_INTERACTION=<span class="number">1</span></span><br><span class="line"><span class="keyword">ENV</span> COMPOSER_HOME=/usr/local/share/composer</span><br><span class="line"><span class="keyword">RUN</span><span class="bash"> mkdir -p /usr/<span class="built_in">local</span>/share/composer \</span></span><br><span class="line"><span class="bash">    &amp;&amp; curl -o /tmp/composer-setup.php https://getcomposer.org/installer \</span></span><br><span class="line"><span class="bash">    &amp;&amp; php /tmp/composer-setup.php --no-ansi --install-dir=/usr/<span class="built_in">local</span>/bin --filename=composer --snapshot \</span></span><br><span class="line"><span class="bash">    &amp;&amp; rm -f /tmp/composer-setup.* \</span></span><br><span class="line"><span class="bash">    <span class="comment"># 配置composer中国全量镜像</span></span></span><br><span class="line">    &amp;&amp; composer config -g repo.packagist composer https://packagist.phpcomposer.com</span><br><span class="line"><span class="keyword">EXPOSE</span> <span class="number">9000</span></span><br><span class="line"><span class="keyword">WORKDIR</span><span class="bash"> /var/www/html</span></span><br><span class="line"><span class="keyword">CMD</span><span class="bash"> [<span class="string">"/usr/local/sbin/php-fpm"</span>, <span class="string">"-F"</span>]</span></span><br></pre></td></tr></table></figure></p>
<h4 id="使用docker-compose来接nginx和php"><a href="#使用docker-compose来接nginx和php" class="headerlink" title="使用docker-compose来接nginx和php"></a>使用docker-compose来接nginx和php</h4><p>此处全部基于上面的镜像<br><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">安装docker-compose</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> curl -L https://github.com/docker/compose/releases/download/1.24.0-rc1/docker-compose-`uname -s`-`uname -m` -o /usr/<span class="built_in">local</span>/bin/docker-compose</span></span><br><span class="line"><span class="meta">#</span><span class="bash">设置docker-compose可执行</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> chmod +x /usr/<span class="built_in">local</span>/bin/docker-compose</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">查看docker-compose是否安装成功</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker-compose --version</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">启动，进入dcoker-compose.yml文件目录下</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker-compose up -d</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">重新启动</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker-compose up -d --force-recreate</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 也可以使用</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker-compose start</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> docker-compose stop</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">使用docker-compose，nginx的配置文件需要修改为</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> /data/nginx/conf.d/www.conf</span></span><br><span class="line"></span><br><span class="line">location ~ \.php &#123;</span><br><span class="line"><span class="meta">		#</span><span class="bash">此处指定项目的文件</span></span><br><span class="line">        root            /var/www/html/project/public;</span><br><span class="line">        #使用服务名php-fpm代替IP</span><br><span class="line">        fastcgi_pass   php-fpm:9000;</span><br><span class="line">        fastcgi_index  index.php;</span><br><span class="line">        fastcgi_split_path_info ^(.+\.php)(.*)$;</span><br><span class="line">        fastcgi_param   PATH_INFO $fastcgi_path_info;</span><br><span class="line">        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;</span><br><span class="line">        include        fastcgi_params;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>docker-compose.yml文件内容<br><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">version:</span> <span class="string">"3"</span></span><br><span class="line"><span class="attr">services:</span></span><br><span class="line">  <span class="attr">nginx:</span></span><br><span class="line">    <span class="attr">restart:</span> <span class="string">always</span></span><br><span class="line">    <span class="attr">image:</span> <span class="string">nginx:latest</span></span><br><span class="line">    <span class="attr">container_name:</span> <span class="string">nginx</span></span><br><span class="line">    <span class="attr">ports:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">"80:80"</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">"443:443"</span></span><br><span class="line">    <span class="attr">volumes:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">/data/nginx/html/:/usr/share/nginx/html/:rw</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">/data/nginx/conf.d:/etc/nginx/conf.d/:ro</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">/data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">/data/nginx/logs/:/var/log/nginx/:rw</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">/etc/localtime:/etc/localtime/:ro</span></span><br><span class="line">    <span class="attr">links:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">php-fpm</span></span><br><span class="line">  <span class="attr">php-fpm:</span></span><br><span class="line">    <span class="attr">restart:</span> <span class="string">always</span></span><br><span class="line">    <span class="attr">build:</span></span><br><span class="line">      <span class="attr">context:</span> <span class="string">.</span></span><br><span class="line">      <span class="attr">dockerfile:</span> <span class="string">Dockerfile</span></span><br><span class="line">    <span class="attr">container_name:</span> <span class="string">php-fpm</span></span><br><span class="line">    <span class="attr">expose:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">"9000"</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">"22026"</span></span><br><span class="line">    <span class="attr">volumes:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">/data/nginx/html:/var/www/html:rw</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">/data/php:/usr/local/etc/php:rw</span></span><br></pre></td></tr></table></figure></p>
<p>使用docker-compose过程发现dockerfile构建的php扩展可能没开启,手动添加ini文件可以开启</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">extension=mysqli.so</span><br><span class="line">extension=redis.so</span><br><span class="line">extension=pdo_mysql.so</span><br><span class="line">extension=swoole.so</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/后端/" rel="tag"># 后端</a>
          
            <a href="/tags/PHP/" rel="tag"># PHP</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/05/06/centos用yum安装redis/" rel="next" title="centos用yum安装redis">
                <i class="fa fa-chevron-left"></i> centos用yum安装redis
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/05/13/前端时间操作工具类/" rel="prev" title="前端时间操作工具类 - JavaScript - 方法封装">
                前端时间操作工具类 - JavaScript - 方法封装 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div id="gitalk-container"></div>

  




        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpeg" alt="ZhuYunlong">
            
              <p class="site-author-name" itemprop="name">ZhuYunlong</p>
              <p class="site-description motion-element" itemprop="description">记录个人在开发过程中遇到的一些问题</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">27</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">9</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#安装docker"><span class="nav-number">1.</span> <span class="nav-text">安装docker</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#docker的一些基本操作"><span class="nav-number">2.</span> <span class="nav-text">docker的一些基本操作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#安装nginx"><span class="nav-number">3.</span> <span class="nav-text">安装nginx</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#安装mysql"><span class="nav-number">4.</span> <span class="nav-text">安装mysql</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#安装redis"><span class="nav-number">5.</span> <span class="nav-text">安装redis</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#安装php7-2"><span class="nav-number">6.</span> <span class="nav-text">安装php7.2</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#使用docker-compose来接nginx和php"><span class="nav-number">7.</span> <span class="nav-text">使用docker-compose来接nginx和php</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">ZhuYunlong</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>

<div>
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
  <span id="busuanzi_container_site_pv" style="display:none">
      本站总访问量 <span id="busuanzi_value_site_pv"></span> 次
      <span class="post-meta-divider">|</span>
  </span>
  <span id="busuanzi_container_site_uv" style="display:none">
      有<span id="busuanzi_value_site_uv"></span>人看过我的博客啦
  </span>
</div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
  <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
  <script src="/js/src/md5.min.js"></script>
   <script type="text/javascript">
        var gitalk = new Gitalk({
          clientID: '',
          clientSecret: '',
          repo: 'blog-comment',
          owner: 'zhuyunlong2018',
          admin: ['zhuyunlong2018'],
            id: md5(location.pathname),
          distractionFreeMode: 'true'
        })
        gitalk.render('gitalk-container')           
       </script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
